package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Attendance;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.AttendanceMapper;
import cn.wolfcode.crm.mapper.EmployeeMapper;
import cn.wolfcode.crm.query.AttendanceQueryObject;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IAttendanceService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;


@Service
public class AttendanceServiceImpl implements IAttendanceService {
    @Autowired
    private AttendanceMapper mapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return mapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Attendance record) {
        return mapper.insert(record);
    }

    @Override
    public Attendance selectByPrimaryKey(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Attendance> selectAll() {
        return mapper.selectAll();
    }

    @Override
    public int updateByPrimaryKey(Attendance record) {
        return mapper.updateByPrimaryKey(record);
    }

    @Override
    public PageResult query(AttendanceQueryObject qo) {
        int total = mapper.queryForCount(qo);
        if (total == 0) {
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        return new PageResult(total, mapper.queryForList(qo));
    }

    /**
     * 签到功能:
     * 1.判断当前的用户是否已经签到,若是已经签到了,则抛一个异常说它已经签到过了
     * 实现:获取到当前登录的用户,判断,然后抛出异常或者返回一个JsonResult
     * 2.若是当前用户没有签到,就对其进行签到处理,也就是通过添加一条attandent数据,并且里面的onState要设置为1
     * 实现:获取到当前用户,根据用户的id修改吧
     * 3.上述的两种情况都要进行时间的比对
     */
    @Override
    public void onState() {
        //获取当前登录的用户
        Employee principal = (Employee) SecurityUtils.getSubject().getPrincipal();
        //根绝当前登录的用户和当前时间的年月日来查询数据库中是否有今天的记录,若是有:
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");//确定了格式
        String newDate = dateFormat.format(date);//将Date类型具体的转换
        Attendance attendance = mapper.selectByEmployeeIdAndNewDate(principal.getId(), newDate);
        //则获取签到记录,若是已经签到,则抛出异常信息,若是没有签到,则修改签到记录为已经签到
        //若是没有签到记录,则插入一条签到记录
        if (attendance != null && attendance.getOnState()) {
            throw new RuntimeException("你今天已经签过到了!!!");
        } else if (attendance != null && !attendance.getOnState()) {
            attendance.setOnState(true);
            attendance.setSignOn(date);
            mapper.updateOnState(attendance);//修改为已经签到
        } else if (attendance == null) {
            //创建Attendance对象
            attendance = new Attendance();
            attendance.setEmployee(principal);
            attendance.setNowDate(new Date());
            attendance.setOnState(true);
            attendance.setSignOn(new Date());
            mapper.insert(attendance);
        }
        //签完到以后将attendance的id共享出去
        SecurityUtils.getSubject().getSession().setAttribute("attendanceId", attendance.getId());
    }

    @Override
    public void downState() {
        //获取当前登录的用户
        Employee principal = (Employee) SecurityUtils.getSubject().getPrincipal();
        //根绝当前登录的用户和当前时间的年月日来查询数据库中是否有今天的记录,若是有:
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");//确定了格式
        String newDate = dateFormat.format(date);//将Date类型具体的转换
        Attendance attendance = mapper.selectByEmployeeIdAndNewDate(principal.getId(), newDate);
        //则判断该记录中是否已经签退:先判断是否签到了,若是没签到何来签退,若是签退了,则抛异常信息,若是没有签退:则修改记录
        if (attendance != null) {
            if (!attendance.getOnState()) {
                throw new RuntimeException("签退失败!!!你都没有签到,怎么能签退!!!");
            } else if (attendance.getOnState()) {
                if (attendance.getDownState()) {
                    throw new RuntimeException("你已经签退过了!!!");
                } else {
                    //给attendance赋值
                    attendance.setDownState(true);
                    attendance.setSignDowm(date);
                    //执行签退方法
                    mapper.updateByPrimaryKey(attendance);
                }
            }
        } else {
            throw new RuntimeException("签退失败!!!你都没有签到,怎么能签退!!!");
        }
    }

    /**
     * 业务分析:
     * 1.根据当前登录的用户id和选择的时间对应nowDate将记录查询出来你
     * 2.判断获取到的记录,若是为空,向里面添加一条数据并设置里面的补签
     * 3.若是不为空,则判断里面的数据是否有签到记录,若是有,则不需要补签,若是没有,则修改
     * 4.判断时间的范围,若是超过了当前时间,则抛异常信息
     *
     * @param selectDate
     */
    @Override
    public void signUp(Date selectDate, Long employeeId) {
        Date date = new Date();//当前时间作为补签时间
        Employee principal = (Employee) SecurityUtils.getSubject().getPrincipal();
        //根绝当前登录的用户和当前时间的年月日来查询数据库中是否有今天的记录,若是有:
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");//确定了格式
        String newDate = dateFormat.format(selectDate);//将Date类型具体的转换
        if (selectDate.before(date)) {
            if (employeeId == null) {
                Attendance attendance = mapper.selectByEmployeeIdAndNewDate(principal.getId(), newDate);
                //为空的时候
                if (attendance == null) {
                    attendance = new Attendance();
                    attendance.setEmployee(principal);
                    attendance.setNowDate(selectDate);
                    attendance.setSignUp(date);
                    mapper.inserSignUp(attendance);
                } else {
                    if (attendance.getOnState()) {
                        throw new RuntimeException("已经签过到了,不能补签了!!!");
                    } else {
                        attendance.setSignUp(date);
                        mapper.updateSignUp(attendance);
                    }
                }
            } else {
                Employee employee = employeeMapper.selectByPrimaryKey(employeeId);
                Attendance attendance = mapper.selectByEmployeeIdAndNewDate(employee.getId(), newDate);
                //为空的时候
                if (attendance == null) {
                    attendance = new Attendance();
                    attendance.setEmployee(employee);
                    attendance.setNowDate(selectDate);
                    attendance.setSignUp(date);
                    mapper.inserSignUp(attendance);
                } else {
                    if (attendance.getOnState()) {
                        throw new RuntimeException("已经签过到了,不能补签了!!!");
                    } else {
                        attendance.setSignUp(date);
                        mapper.updateSignUp(attendance);
                    }
                }
            }
        } else {
            throw new RuntimeException("你竟然还想给未来补签!!!");
        }
    }
}
